<!DOCTYPE html>

<html>
<head>
	<meta charset="utf-8" />
	<link rel="stylesheet" type="text/css" href="style.css" />
	<title>Catégories</title>

</head>
<body>

	<?php
		session_start();
	?>

	<div class="top-menu">
		<ul>
			<li><a href="index.php" >Accueil</a></li>
			<li><a href="affCategorie.php">Catégories</a></li>
			<li><a href="#">Link 3</a></li>
			<li><a href="#">Link 4</a></li>
			<li><a href="#">Link 5</a></li>
		</ul>
		
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
		<div class="login-box">
		<form action="connexion.php" method="post">
			Connexion :<input type="text" name="login" placeholder="Login" />
			<input type="text" name="mdp" placeholder="Password" />
			<input type="submit" value="OK">
		</form>
		</div>
		<?php
		}
		else
		{
			?>
			<div class="login-box">Vous etes connecté en tant que <?php
			if (isset($_SESSION) && isset($_SESSION["login"]))
				echo '<div class="login-name">' . $_SESSION["login"] . '</div>';
			?>
			<a href="sdestr.php">Déconnexion</a>
			</div>
			<?php
		}
		?>
		<br />
	</div>
	<div class="info-box-frame">
	<div class="info-box">
		<?php
			if(!isset($_GET['page']))
				{
		?>
		<?php
		if (!isset($_SESSION) || !isset($_SESSION['connected']) || !$_SESSION['connected'] == true)
		{
			?>
			<div class="info-box-title">Nouveau sur le forum ? Inscrivez vous vite !<br /></div>
		
					<form action="inscription.php?page=1" method="post">
						<input type="text" name="login" placeholder="Login" />
						<input type="text" name="mdp" placeholder="Password"/>
						<input type="text" name="email" placeholder="Mail address"/>
						<input type="text" name="nom" placeholder="Name"/>
						<input type="text" name="prenom" placeholder="Frist name"/>						
						<input type="submit" value="OK">
					</form>
				<?php
				}
				?>
		<?php
				}
			else 
				{
					$error = false;
					if (strlen($_POST['login']) < 3 || strlen($_POST['login']) > 255 || empty($_POST['login']))
						{
							$errorToAff = '<p>Le login n\'est pas conforme<p>';
							$error = true;
						}
					if (strlen($_POST['mdp']) < 3 || strlen($_POST['mdp']) > 255 || empty($_POST['mdp']))
						{
							$errorToAff .= '<p>Le mot de passe n\'est pas conforme</p>';
							$error = true;
						}
					if (!preg_match("#^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email']) || empty($_POST['email']))
						{
							$errorToAff .= '<p>L\'email n\'est pas conforme</p>';
							$error = true;
						}
					if (empty($_POST['nom']))
						{
							$errorToAff .= '<p>Veuillez entrer un nom</p>';
							$error = true;
						}
					if (empty($_POST['prenom']))
						{
							$errorToAff .= '<p>Veuillez entrer un prenom</p>';
							$error = true;
						}
					if ($error)
						{
							echo $errorToAff;
							echo '</ br><a href="inscription.php">Retour a l\'inscription</a>';
						}
					else 
						{
							$mysqli = new mysqli("localhost", "root", "vlvlvl59150", "forum");

							/* Vérification de la connexion */
							if (mysqli_connect_errno())
								{
    								echo 'Échec de la connexion : ' . mysqli_error();
    								exit;
								}
							$requete = 'INSERT INTO user (login, mdp, email, nom, prenom, isAdmin, isModo) VALUES (\'' . $_POST['login'] . '\', \'' . sha1($_POST['mdp']) . '\', \'' . $_POST['email'] . '\', \'' . $_POST['nom'] . '\', \'' . $_POST['prenom'] . '\', \'0\', \'0\');';
							if ($result = $mysqli->query($requete))
								{			
									echo '</ br><a href="login.php">Vous etes inscrit veuillez vous connectez maintenant</a>';
									$result->close();
								}
							else 
								{
									echo '<p>La requette a echouer</p>';
								}
							$mysqli->close();
						}
				}
		?>
	</div>
</div>
	<!-- BEGIN CATBOX BLOCK********************************************************************************************************************** -->
	<div class="catbox">
	</div>
	<!-- END CATBOX BLOCK********************************************************************************************************************** -->
	<footer>
			<p>
				[/!\FIXME/!\	INSERT_PHP_FOR_NB_PAGES]
			</p>
	</footer>

</body>

</html>